package com.cash.app.ui.other

import android.content.Context
import android.net.Uri
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.cash.app.base.BaseViewModel
import com.cash.app.net.NetRequestExecutor
import com.cash.app.net.RetrofitClient
import com.cash.app.ui.other.api.FeedbackAPIService
import com.cash.app.ui.other.model.SubmitFeedbackRequest
import com.cash.app.utils.ImageBase64Utils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

class FeedbackSubmitViewModel : BaseViewModel() {

    private val feedbackAPIService by lazy {
        RetrofitClient.createService(FeedbackAPIService::class.java)
    }

    private val _submitSuccess = MutableLiveData<Boolean>()
    val submitSuccess: LiveData<Boolean> = _submitSuccess

    private val _isLoading = MutableLiveData<Boolean>()
    val isLoading: LiveData<Boolean> = _isLoading

    fun submitFeedback(
        context: Context,
        phoneNumber: String,
        content: String,
        photos: List<Uri>,
        feedbackType: String = ""
    ) {
        launchRequest(request = {
            val base64Photos = withContext(Dispatchers.IO) {
                ImageBase64Utils.convertImagesToBase64(context, photos)
            }

            val request = SubmitFeedbackRequest(
                phone = phoneNumber,
                comment = content,
                photo = base64Photos,
                feedbackTypes = listOf(feedbackType)
            )

            NetRequestExecutor.baseNetRequest {
                feedbackAPIService.submitFeedback(request)
            }
        }, onSuccess = { // rsp is empty
            _submitSuccess.value = true
        }, onError = { _, _ ->
        }, onException = { _, _ ->
        })
    }
} 